<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button id="setCookie">设置 Cookie</button>
  <script>

    // var docCookies = {
    //   get: function (key) {
    //     return decodeURIComponent(
    //       document.cookie.replace(
    //         new RegExp('(?:(?:^|.*;)\\s*' + encodeURIComponent(key).replace(/[-.+*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1')
    //     ) || ''
    //   },
    //   set: function (key, sValue, vEnd, sPath, sDomain, bSecure) {
    //     if (!key || /^(?:expires|max-age|path|domain|secure)$/i.test(key)) { return false }
    //     let sExpires = ''
    //     if (vEnd) {
    //       switch (vEnd.constructor) {
    //         case Number:
    //           sExpires = vEnd === Infinity ? '; expires=Fri, 31 Dec 9999 23:59:59 GMT' : '; max-age=' + vEnd
    //           break
    //         case String:
    //           sExpires = '; expires=' + vEnd
    //           break
    //         case Date:
    //           sExpires = '; expires=' + vEnd.toUTCString()
    //           break
    //       }
    //     }
    //     document.cookie = encodeURIComponent(key) + '=' + encodeURIComponent(sValue) + sExpires + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : '') + (bSecure ? '; secure' : '')
    //     return true
    //   },
    //   remove: function (key, sPath = '/', sDomain = '.datatool.vip') {
    //     if (!key || !this.has(key)) { return false }
    //     document.cookie = encodeURIComponent(key) + '=; expires=' + new Date('1970-01-01').toString() + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : '')
    //     return true
    //   },
    //   has: function (key) {
    //     return (new RegExp('(?:^|;\\s*)' + encodeURIComponent(key).replace(/[-.+*]/g, '\\$&') + '\\s*\\=')).test(document.cookie)
    //   },
    //   keys: function () {
    //     let keys = document.cookie.replace(/((?:^|\s*;)[^=]+)(?=;|$)|^\s*|\s*(?:=[^;]*)?(?:1|$)/g, '').split(/\s*(?:=[^;]*)?;\s*/)
    //     for (let idx = 0; idx < keys.length; idx++) { keys[idx] = decodeURIComponent(keys[idx]) }
    //     return keys
    //   }
    // }

    class AnCookie {
      get(key) {
        return decodeURIComponent(
          document.cookie.replace(
            new RegExp('(?:(?:^|.*;)\\s*' + encodeURIComponent(key).replace(/[-.+*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1')
        ) || ''
      }

      set({ key, value, expires = 0, path = '/', domain = '', secure = false } = {}) {
        if (!key) {
          return false;
        }
        let rc = [];
        rc.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
        if (expires instanceof Date) {
          rc.push(`expires=${expires.toUTCString()}`);
        } else if (typeof expires === 'number') {
          const date = new Date();
          date.setTime(date.getTime() + expires * 1000);
          rc.push(`expires=${date.toUTCString()}`);
        }
        if (path) {
          rc.push(`path=${path}`);
        }
        if (domain) {
          rc.push(`domain=${domain}`);
        }
        if (secure) {
          rc.push(`secure`);
        }
        rc = rc.join('; ');
        console.log(rc);
        document.cookie = rc;
        return true;
      }

      has(key) {
        if (!key) {
          return false;
        }
        return (new RegExp('(?:^|;\\s*)' + encodeURIComponent(key).replace(/[-.+*]/g, '\\$&') + '\\s*\\=')).test(document.cookie)
      }

      remove(options = {}) {
        if (!this.has(options.key)) {
          return false;
        }
        return this.set({
          ...options,
          expires: new Date('1970-01-01').toUTCString()
        })
      }

      keys() {
        let keys = document.cookie.replace(/((?:^|\s*;)[^=]+)(?=;|$)|^\s*|\s*(?:=[^;]*)?(?:1|$)/g, '').split(/\s*(?:=[^;]*)?;\s*/)
        for (let idx = 0; idx < keys.length; idx++) {
          keys[idx] = decodeURIComponent(keys[idx])
        }
        return keys
      }
    }
    const anCookie = new AnCookie();
    // http://local.sddtiktok.com:5500/%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/cookie.html
    document.getElementById('setCookie').onclick = () => {
      anCookie.set({
        key: 'Plugin-Token',
        value: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vZGF0YXRvb2wudmlwIiwiYXVkIjoiaHR0cDovL2RhdGF0b29sLnZpcCIsImp0aSI6ImRiMmQwODRhMjlqaSIsImlhdCI6MTczNzM2MTk4NCwiZXhwIjoxNzM3NDQ4Mzg0LCJ1aWQiOiI3MDE2MDE4NDA1MDgxNzQzMzYiLCJuaWNrbmFtZSI6ImhlcGluZyIsImxvZ2luX25hbWUiOiIxMjEzNTgyOTAyQHFxLmNvbSIsImRhdGF0b29sIjoxfQ.dWtYHHIKAdp-4fIdGOGQKXcG0Lbvr91sevzyqCekAOE',
        expires: 500 * 1000,
        path: '/',
        domain: '.sddtiktok.com',
        secure: false
      })
    }
  </script>
</body>

</html>